//
//  NavigationHelper.m
//  FreshToHome
//
//  Created by lzh on 2017/11/16.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "NavigationHelper.h"
#import "MJRefresh.h"
#import "MJExtension.h"

@implementation NavigationHelper

#pragma mark - root

+ (UIWindow*)rootWindow
{
    __block UIWindow *window;
    [[[UIApplication sharedApplication] windows] enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ( window.windowLevel == UIWindowLevelNormal) {
            window = obj;
            *stop = YES;
        }
        
    }];
    
    return window;
}

+ (UINavigationController*)rootNavi
{
    UIWindow* rootWindow = [self rootWindow];
    
    return (UINavigationController*)rootWindow.rootViewController;
}

+ (UINavigationController*)currentNavi
{
    UIViewController *controller = [self rootNavi];
    
    while (controller.presentedViewController)
    {
        controller = controller.presentedViewController;
        
    }
    
    if ([controller isKindOfClass:[UINavigationController class]])
    {
        return (UINavigationController *)controller;
    }
    else
    {
        return controller.navigationController;
    }
}


#pragma mark - push 操作
+ (void)routeTo:(NSString*)classStr
{
    [self routeTo:classStr withInfo:nil];
}
+ (void)routeTo:(NSString*)classStr withInfo:(id) object
{
    [self routeTo:classStr withInfo:object removeVC:nil];
}
+ (void)routeTo:(NSString*)classStr withInfo:(id) object removeVC:(UIViewController*)vc
{
    [self routeTo:classStr withInfo:object removeVC:vc animated:YES];
}
+ (void)routeTo:(NSString*)classStr withInfo:(id) object removeVC:(UIViewController*)vc animated:(BOOL) animated
{
    Class ViewController = NSClassFromString(classStr);
    
    if (!ViewController) {
        return;
    }
    
    UIViewController* aViewController = [[ViewController alloc]init];

    if (object && [aViewController conformsToProtocol:@protocol(NavigationModel)]) {
        
        [aViewController setValue:object forKey:@"intentData"];
        
    }
    
    if([object isKindOfClass:[NSDictionary class]]){
        NSDictionary* params = object;
        
        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
           
            [aViewController.class mj_enumerateProperties:^(MJProperty *property, BOOL *stop) {
                
                if ([property.name isEqualToString:key]) {
                    [aViewController setValue: obj forKey:key];
                    *stop = YES;
                }
                
            }];
            
            
            
        }];
    }
    
    NSArray* viewControllers = [self currentNavi].viewControllers;
    
    NSMutableArray* mutableViewControllers = [NSMutableArray new];
    
    if (viewControllers.count > 0) {
        
        mutableViewControllers = [NSMutableArray arrayWithArray: viewControllers ];
    }
    
    if (vc) {
        if ([mutableViewControllers containsObject:vc]) {
            [mutableViewControllers removeObject:vc];
        }
        
    }
    
    [mutableViewControllers addObject:aViewController];
    
    [[self currentNavi] setViewControllers:mutableViewControllers animated:animated];
    
}

//+ (void)routeToWebWithAction:(RequestAction*)action shareDomain:(ShareDomain*)shareDomain
//{
//    [self routeToWebWithAction:action shareDomain:shareDomain removeVC:nil];
//}
//
//
//+ (void)routeToWebWithAction:(RequestAction*)action shareDomain:(ShareDomain*)shareDomain removeVC:(UIViewController*)vc
//{
//    if (action && shareDomain) {
//        [self routeTo:@"BaseWebViewController" withInfo:@{
//                                                          @"requestAction" : action,
//                                                          @"shareDomain" : shareDomain
//                                                          } removeVC:vc];
//    }
//    else if(action)
//    {
//        [self routeTo:@"BaseWebViewController" withInfo:@{
//                                                          @"requestAction" : action,                                                          } removeVC:vc];
//    }
//
//}
//
//+ (void)routeToWebWithAction:(RequestAction*)action{
//    [self routeToWebWithAction:action shareDomain:nil];
//}

#pragma mark - pop 操作
+ (void)pop
{
    [self popWithAnimate:YES];
}
+ (void)popWithAnimate:(BOOL)animate
{
    [[self currentNavi] popViewControllerAnimated:animate];

}
+ (void)popToRoot
{
    [self popToRootWithAnimate:YES];
}
+ (void)popToRootWithAnimate:(BOOL)animate
{
    [[self currentNavi]popToRootViewControllerAnimated:animate];
}
+ (void)popToVC:(NSString*)classStr
{
    [self popToVC:classStr WithAnimate:YES];

}

+ (void)popToVC:(NSString*)classStr WithAnimate:(BOOL)animate
{
    Class class = NSClassFromString(classStr);
    
    if (!class) {
        return;
    }
    
    NSArray* viewControllers = [self currentNavi].viewControllers;
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController* vc = viewControllers[i];
        if ([vc isKindOfClass:class]) {
            [[self currentNavi]popToViewController:vc animated:animate];
            break;
        }
    }
}

#pragma mark - current vc

+ (UIViewController*)visibleViewController
{
    UIViewController* vc = [self currentNavi];
    
    if (vc && [vc isKindOfClass:[UINavigationController class]]) {
        return ((UINavigationController*)vc).visibleViewController;
    }
    else if(vc && [vc isKindOfClass:[UIViewController class]])
    {
        return vc;
    }
    
    return nil;
}

+ (UIViewController*)rootTabViewController{
    UINavigationController* navi = [self rootNavi];
    return navi.viewControllers.firstObject;
}

#pragma mark - other

////没有拿到用户的手机。
//+ (void)routeToUserGuide{
//
//    BOOL hasBodyTest = NO;
//    NSDictionary* info = [SettingDataHelper dictionaryOfKey:[GlobalDataCenter shareInstance].userInfo.id];
//    if (info && [info.allKeys containsObject:BodyTestGuide]) {
//        hasBodyTest = [[info objectForKey:BodyTestGuide] boolValue];
//    }
//
//    if (![GlobalDataCenter shareInstance].isbindPhone && !hasBodyTest) {
//
//        GuideWelcomeViewController *guideVC = [[GuideWelcomeViewController alloc]init];
//        VerticalNavigationController *baseNV = [[VerticalNavigationController alloc]initWithRootViewController:guideVC];
//        baseNV.businessType = VerticalNaviBusinessType_UserGuide;
//        [[self visibleViewController] presentViewController:baseNV animated:NO completion:nil];
//    }
//}
//
//+ (void)routeToValidateUser
//{
//
//    if (![GlobalDataCenter shareInstance].isbindPhone) {
//        [self routeToLoginViewController];
//    }
//
////    if (![GlobalDataCenter shareInstance].isLogin) {
////        [self routeToLoginViewController];
////    }
////    else if (![GlobalDataCenter shareInstance].isActivite){
////        [self routeToAccountActivateViewController];
////    }
//}
//
//+ (void)routeToLoginViewController
//{
//    LoginViewController *loginVC = [[LoginViewController alloc]init];
//    VerticalNavigationController *baseNV = [[VerticalNavigationController alloc]initWithRootViewController:loginVC];
//    [[self visibleViewController] presentViewController:baseNV animated:YES completion:nil];
//
//}
//
//+ (void)routeToAccountActivateViewController
//{
//    AccountActivateViewController *activateVC = [[AccountActivateViewController alloc]init];
//    VerticalNavigationController *baseNV = [[VerticalNavigationController alloc]initWithRootViewController:activateVC];
//    [[self visibleViewController] presentViewController:baseNV animated:YES completion:nil];
//
//}
//
//+ (void)routeToBindWeChatPhoneViewControllerWithOpenId:(NSString*)openId
//{
//    LoginViewController *bindVC = [[LoginViewController alloc]init];
//    UMSocialUserInfoResponse* socialResp = openId;
//
//    [bindVC setValue:@(2) forKey:@"businessType"];
//    [bindVC setValue:socialResp.openid forKey:@"openId"];
//    [bindVC setValue:socialResp.unionId forKey:@"unionId"];
//    [bindVC setValue:socialResp.name.length ? socialResp.name : @"" forKey:@"name"];
//    [bindVC setValue:socialResp.iconurl.length ? socialResp.iconurl : @"" forKey:@"iconurl"];
//
//    [[NavigationHelper currentNavi]pushViewController:bindVC animated:YES];
//
////    VerticalNavigationController *baseNV = [[VerticalNavigationController alloc]initWithRootViewController:bindVC];
////    [[self visibleViewController] presentViewController:baseNV animated:YES completion:nil];
//}

#pragma mark - present

+ (void)presentVC:(NSString*)classStr withInfo:(id) object{
    
    Class ViewController = NSClassFromString(classStr);
    
    if (!ViewController) {
        return;
    }
    
    UIViewController* aViewController = [[ViewController alloc]init];
    
    if (object && [aViewController conformsToProtocol:@protocol(NavigationModel)]) {
        
        [aViewController setValue:object forKey:@"intentData"];
        
    }
    
    if([object isKindOfClass:[NSDictionary class]]){
        NSDictionary* params = object;
        
        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            
            [aViewController.class mj_enumerateProperties:^(MJProperty *property, BOOL *stop) {
                
                if ([property.name isEqualToString:key]) {
                    [aViewController setValue: obj forKey:key];
                    *stop = YES;
                }
                
            }];
            
            
            
        }];
    }
    
    BaseNavigationController* verticalVC = [[BaseNavigationController alloc]initWithRootViewController:aViewController];
    
    [[self visibleViewController]presentViewController:verticalVC animated:YES completion:nil];
}
@end
